cmake_minimum_required(VERSION 3.1)
project(flypcl)

find_package(catkin REQUIRED COMPONENTS
  pcl_ros
  pcl_conversions
  roscpp
  roslint
  sensor_msgs
  tf2_ros
  std_msgs
  rospy
  rviz
  visualization_msgs
)

# 声明catkin包
catkin_package(
  CATKIN_DEPENDS
  pcl_ros
  pcl_conversions
  roscpp
  roslint
  sensor_msgs
  tf2_ros
  std_msgs
)

include_directories(
  ${catkin_INCLUDE_DIRS}
  /opt/ros/noetic/include
  ${YAML_CPP_INCLUDE_DIRS}
)

# 查找依赖库
find_package(Boost REQUIRED COMPONENTS system filesystem thread)

# 查找 YAML-cpp 库
find_package(PkgConfig REQUIRED)
pkg_check_modules(YAML_CPP REQUIRED yaml-cpp)

# 使用自定义OpenCV
set(OpenCV_DIR "/home/firefly/Software/opencv-4.11.0/build")
find_package(OpenCV 4.11 REQUIRED)
message(STATUS "OpenCV库版本: ${OpenCV_VERSION}")
message(STATUS "OpenCV库路径: ${OpenCV_DIR}")
message(STATUS "OpenCV包含目录: ${OpenCV_INCLUDE_DIRS}")
message(STATUS "OpenCV库文件: ${OpenCV_LIBS}")

# 使用自定义PCL库 - 强制指定路径以避免系统库冲突
set(PCL_DIR "/home/firefly/Software/pcl-1.14.1/build/share/pcl-1.14")
set(CMAKE_PREFIX_PATH "/home/firefly/Software/pcl-1.14.1/build;${CMAKE_PREFIX_PATH}")

# 明确指定PCL库和头文件路径
set(PCL_ROOT "/home/firefly/Software/pcl-1.14.1/build")
set(PCL_INCLUDE_DIRS "/home/firefly/Software/pcl-1.14.1/include")
set(PCL_LIBRARY_DIRS "/home/firefly/Software/pcl-1.14.1/build/lib")

# 明确指定所需的PCL库文件，避免自动搜索导致的冲突
set(PCL_CUSTOM_LIBRARIES
    ${PCL_LIBRARY_DIRS}/libpcl_common.so
    ${PCL_LIBRARY_DIRS}/libpcl_io.so
    ${PCL_LIBRARY_DIRS}/libpcl_filters.so
    ${PCL_LIBRARY_DIRS}/libpcl_segmentation.so
    ${PCL_LIBRARY_DIRS}/libpcl_search.so
    ${PCL_LIBRARY_DIRS}/libpcl_sample_consensus.so
    ${PCL_LIBRARY_DIRS}/libpcl_features.so
    ${PCL_LIBRARY_DIRS}/libpcl_octree.so
    ${PCL_LIBRARY_DIRS}/libpcl_registration.so
    ${PCL_LIBRARY_DIRS}/libpcl_kdtree.so
)

find_package(PCL 1.14 REQUIRED COMPONENTS common io filters segmentation search sample_consensus features octree registration)
include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

# 添加PCL模板实例化标志
add_definitions(-DPCL_NO_PRECOMPILE)

message(STATUS "PCL库版本: ${PCL_VERSION}")
message(STATUS "PCL包含目录: ${PCL_INCLUDE_DIRS}")
message(STATUS "PCL库目录: ${PCL_LIBRARY_DIRS}")
message(STATUS "PCL自定义库文件: ${PCL_CUSTOM_LIBRARIES}")

# 0711
# 添加s2020detect节点的编译与链接配置
add_executable(s2020detect 
              src/s2020detect.cpp 
              src/my_pcl/my_pcl.cpp
              src/mcu_correspond/mcu_data_sender.cpp
              src/mcu_correspond/mcu_data_subscriber.cpp
)

# 为s2020detect设置明确的库路径，优先使用自定义PCL
target_include_directories(s2020detect PRIVATE 
    ${PCL_INCLUDE_DIRS}
    ${catkin_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
)

target_link_directories(s2020detect PRIVATE 
    ${PCL_LIBRARY_DIRS}
)

target_link_libraries(s2020detect 
    ${catkin_LIBRARIES}
    ${PCL_CUSTOM_LIBRARIES}
    ${Boost_LIBRARIES}
    ${OpenCV_LIBRARIES} 
    ${YAML_CPP_LIBRARIES}
)

# 确保使用正确的RPATH
set_target_properties(s2020detect PROPERTIES
    INSTALL_RPATH "${PCL_LIBRARY_DIRS}"
    BUILD_WITH_INSTALL_RPATH TRUE
)

# 0714
add_executable(s2022circle
              src/s2022circle.cpp 
              src/my_pcl/my_pcl.cpp
              src/mcu_correspond/mcu_data_sender.cpp
              src/mcu_correspond/mcu_data_subscriber.cpp
)

# 为s2022circle设置明确的库路径，优先使用自定义PCL
target_include_directories(s2022circle PRIVATE 
    ${PCL_INCLUDE_DIRS}
    ${catkin_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
)

target_link_directories(s2022circle PRIVATE 
    ${PCL_LIBRARY_DIRS}
)

target_link_libraries(s2022circle 
    ${catkin_LIBRARIES}
    ${PCL_CUSTOM_LIBRARIES}
    ${Boost_LIBRARIES}
    ${OpenCV_LIBRARIES} 
    ${YAML_CPP_LIBRARIES}
)

# 确保使用正确的RPATH，避免运行时库冲突
set_target_properties(s2022circle PROPERTIES
    INSTALL_RPATH "${PCL_LIBRARY_DIRS}"
    BUILD_WITH_INSTALL_RPATH TRUE
)

# g2021杆子检测节点
add_executable(g2021stick
              src/g2021stick.cpp 
              src/my_pcl/my_pcl.cpp
              src/mcu_correspond/mcu_data_sender.cpp
              src/mcu_correspond/mcu_data_subscriber.cpp
)
target_include_directories(g2021stick PRIVATE 
    ${PCL_INCLUDE_DIRS}
    ${catkin_INCLUDE_DIRS}
    ${OpenCV_INCLUDE_DIRS}
)
target_link_directories(g2021stick PRIVATE 
    ${PCL_LIBRARY_DIRS}
)
target_link_libraries(g2021stick 
    ${catkin_LIBRARIES}
    ${PCL_CUSTOM_LIBRARIES}
    ${Boost_LIBRARIES}
    ${OpenCV_LIBRARIES} 
    ${YAML_CPP_LIBRARIES}
)
set_target_properties(g2021stick PROPERTIES
    INSTALL_RPATH "${PCL_LIBRARY_DIRS}"
    BUILD_WITH_INSTALL_RPATH TRUE
)


roslint_cpp()
roslint_add_test()
